package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.PermissionsChecker;
import com.diudiutechnology.www.yadiudiu.util.utils.PictureUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.SelectPhotoPop;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by Administrator on 2017/8/24.
 * 0、学历 1、驾驶证  2、行驶证
 */

public class IdentityActivity extends Activity implements View.OnClickListener ,Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.title_center)//标题
    TextView title_center;

    @Bind(R.id.identity)
    LinearLayout identity;

    @Bind(R.id.identification_card)//图片标题
    TextView identification_card;

    @Bind(R.id.name)//名字
            EditText name;
    @Bind(R.id.number)//身份证
            EditText number;

    @Bind(R.id.line)//证件号下面的线
            TextView line;

    @Bind(R.id.identification_layout)//图片
    LinearLayout identification_layout;

    @Bind(R.id.id_card_is)//图片正面
    ImageView id_card_is;

    @Bind(R.id.id_card_back)//图片反面
            ImageView id_card_back;

    @Bind(R.id.button)//提交
            Button button;

    Context context;
    private String pos="0";//0、学历 1、驾驶证  2、行驶证
    private MyProgressDialog mDialog;
    MineFragmentModel model;//缓存数据
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private SelectPhotoPop selectPhotoPop;
    private View contentView;
    String Judge="0";//判断哪图片  0、身份证正面  1、身份证反面
    String conimg="";//图片路径
    String justimg="";//正面图片路径
    String backimg="";//反面图片路径
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = View.inflate(this, R.layout.activity_identity,
                null);
        setContentView(contentView);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        id_card_is.setOnClickListener(this);
        id_card_back.setOnClickListener(this);
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        if (getIntent()!=null){
            pos=getIntent().getStringExtra("pos");
        }
        model = UserInfoUtils.readUserInfo(this);
        if (pos.equals("0")){
            title_center.setText(getResources().getString(R.string.education));
            identification_layout.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }else if (pos.equals("1")){
            title_center.setText(getResources().getString(R.string.Driving_licence));
            identity.setVisibility(View.GONE);
            identification_card.setText(getResources().getString(R.string.driver_license));
            //驾驶证正面
            if (!Sundry.getStinges(model.getDrivinglicense())){
                justimg=model.getDrivinglicense();
                imageLoader.displayImage(Constant.HTTP+model.getDrivinglicense(), id_card_is, options);
            }else {
                id_card_is.setImageResource(R.mipmap.dridge_front);
            }
            //驾驶证反面
            if (!Sundry.getStinges(model.getDrivinglicenseback())){
                backimg=model.getDrivinglicenseback();
                imageLoader.displayImage(Constant.HTTP+model.getDrivinglicenseback(), id_card_back, options);
            }else {
                id_card_back.setImageResource(R.mipmap.driving_license);
            }
        }else  if (pos.equals("2")){
            title_center.setText(getResources().getString(R.string.Driving_permit));
            identity.setVisibility(View.GONE);
            identification_card.setText(getResources().getString(R.string.driving_permit));
            //行驶证正面
            if (!Sundry.getStinges(model.getDrivingpermitface())){
                justimg=model.getDrivingpermitface();
                imageLoader.displayImage(Constant.HTTP+model.getDrivingpermitface(), id_card_is, options);
            }else {
                id_card_is.setImageResource(R.mipmap.license_is_positive);
            }
            //行驶证反面
            if (!Sundry.getStinges(model.getDriveback())){
                backimg=model.getDriveback();
                imageLoader.displayImage(Constant.HTTP+model.getDriveback(), id_card_back, options);
            }else {
                id_card_back.setImageResource(R.mipmap.license_is_reverse);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.button:
                if (pos.equals("0")){
                    if (TextUtils.isEmpty(name.getText().toString().trim())){//姓名
                        ToastUtils.show(context, getResources().getString(R.string.please_cardholder));
                        return;
                    }
                    if (TextUtils.isEmpty(number.getText().toString().trim())){//身份证号
                        ToastUtils.show(context, getResources().getString(R.string.please_id_number));
                        return;
                    }
                    else if (!Sundry.personIdValidation(number.getText().toString())){//正确的身份证号
                        ToastUtils.show(this,getResources().getString(R.string.please_correct_id_number));
                        return;
                    }
                    processdata();
                }else {
                    if (Sundry.getStinges(justimg)){//正面
                        if (pos.equals("1")){
                            ToastUtils.show(context, getResources().getString(R.string.please_driving_front));
                        }else    if (pos.equals("2")){
                            ToastUtils.show(context, getResources().getString(R.string.please_travel_front));
                        }
                        return;
                    }
                    if (Sundry.getStinges(backimg)){    //反面
                        if (pos.equals("1")){
                            ToastUtils.show(context, getResources().getString(R.string.please_driving_reverse));
                        }else    if (pos.equals("2")){
                            ToastUtils.show(context, getResources().getString(R.string.please_travel_reverse));
                        }
                        return;
                    }
                    finish();
                    ToastUtils.show(context, getResources().getString(R.string.successfully));
                }
                break;
            case R.id.id_card_is://正面

                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                        if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {
                            Judge="0";
                            showSelectPicPop();
                        } else {
                            ToastUtils.show(context, "请检查是否打开储存权限");
                        }
                    } else {
                        ToastUtils.show(context, "请检查是否打开相机权限");
                    }
                break;
            case R.id.id_card_back://反面
                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                        if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {

                            Judge="1";
                            showSelectPicPop();
                        } else {
                            ToastUtils.show(context, "请检查是否打开储存权限");
                        }
                    } else {
                        ToastUtils.show(context, "请检查是否打开相机权限");
                    }

                break;
                 		/* 注释  相机*/
            case R.id.item_popupwindows_camera:
                selectPhotoPop.dismiss();
                GalleryFinal.openCamera(Constant.CAMERA, MyApplication.getInstance().initGalley(Constant.CAMERA,1), mOnHanlderResultCallback);
                break;
            /* 注释  相册*/
            case R.id.item_popupwindows_Photo:
                selectPhotoPop.dismiss();
                GalleryFinal.openGallerySingle(Constant.GALLERYSINGLE, MyApplication.getInstance().initGalley(Constant.GALLERYSINGLE,1), mOnHanlderResultCallback);
                break;
            /* 注释  取消*/
            case R.id.item_popupwindows_cancel:
                selectPhotoPop.dismiss();
                break;
        }
    }
    //相册
    private void showSelectPicPop() {
        selectPhotoPop = new SelectPhotoPop(this, this);
        selectPhotoPop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            Bitmap bitmap = PictureUtil.decodeSampledBitmapFromFile(resultList.get(0).getPhotoPath(), 400, 400);
            conimg   =PictureUtil.saveBitmapFile(bitmap, String.valueOf(0));
            processadd();
        }
        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };
    private  void processadd(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
                String str="justimg";
                if (Judge.equals("0")){//正面
                    str="justimg";
                }else {//反面
                    str="backimg";
                }
                if (pos.equals("1")){//驾驶
                    HttpClient.addcarid(str, conimg, this);
                }else if (pos.equals("2")){//行驶
                    HttpClient.addtravel(str, conimg, this);
                }
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //提交数据
    public void processdata(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
               HttpClient.education(number.getText().toString().trim(),name.getText().toString().trim(),this);
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (pos.equals("0")){

                    ToastUtils.show(context, getMsg);
                    UserInfoUtils.getInstans().updateis_educationcerti(context,"2");
                    finish();
                }else if (pos.equals("1")){//驾驶
                    String content = getnote.get(0).getContent();
                    if (Judge.equals("0")){//正面
                        justimg=content;
                        imageLoader.displayImage(Constant.HTTP+content, id_card_is, options);
                        UserInfoUtils.getInstans().updatedrivinglicense(context,content);
                    }else {//反面
                        backimg=content;
                        imageLoader.displayImage(Constant.HTTP+content, id_card_back, options);
                        UserInfoUtils.getInstans().updatedrivinglicenseback(context,content);
                    }

                    ToastUtils.show(context, getMsg);
                }else if (pos.equals("2")){//行驶
                    String content = getnote.get(0).getContent();
                    if (Judge.equals("0")){//正面
                        justimg=content;
                        imageLoader.displayImage(Constant.HTTP+content, id_card_is, options);
                        UserInfoUtils.getInstans().updatedrivingpermitface(context,content);
                    }else {//反面
                        backimg=content;
                        imageLoader.displayImage(Constant.HTTP+content, id_card_back, options);
                        UserInfoUtils.getInstans().updatedriveback(context,content);
                    }

                    ToastUtils.show(context, getMsg);
                }
            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            }else {

                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
